﻿Imports Microsoft.VisualBasic
Imports System.Data
Imports System.Web.Script.Serialization
Imports Newtonsoft.Json

Public Class ParserJSON
    Public Shared Function DataSetParserJSON(ByVal ds As DataSet) As String
        Dim rows As New List(Of Dictionary(Of String, Object))
        For Each dr As DataRow In ds.Tables(0).Rows
            Dim row As New Dictionary(Of String, Object)
            For Each dc As DataColumn In dr.Table.Columns
                row.Add(dc.ColumnName, dr(dc.ColumnName))
            Next
            rows.Add(row)
        Next
        Dim jss As New JavaScriptSerializer
        Return jss.Serialize(rows)
    End Function
    Public Shared Function DataTableParserJSON(ByVal dt As DataTable) As String
        Return JsonConvert.SerializeObject(dt, Formatting.Indented)
    End Function
    Public Shared Function JSONParserDataTable(ByVal js As String) As DataTable
        Dim dt As DataTable = JsonConvert.DeserializeObject(Of DataTable)(js)
        Return dt
    End Function
End Class
